package com.fenqing.leetcode.code.t400_500;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

/**
 * @author fenqing
 */
public class Topic498 extends Topic {


    /**
     * 构造器
     *
     * @param input 输入
     */
    public Topic498(String input) {
        super(input
        );
    }

    @Execute
    public int[] findDiagonalOrder(int[][] mat) {
        int row = mat.length, col = mat[0].length;
        int[] result = new int[row * col];
        int direction = 1, x = 0, y = 0, index = 0;
        do {
            result[index++] = mat[x][y];
            if (direction == 1) {
                if (x == 0) {
                    direction = 2;
                    if (y == col - 1) {
                        x++;
                    } else {
                        y++;
                    }
                } else if (y == col - 1) {
                    direction = 2;
                    x++;
                } else {
                    x--;
                    y++;
                }
            } else {
                if (y == 0) {
                    direction = 1;
                    if (x == row - 1) {
                        y++;
                    } else {
                        x++;
                    }
                } else if (x == row - 1) {
                    direction = 1;
                    y++;
                } else {
                    x++;
                    y--;
                }
            }
        } while (x < row && y < col);
        return result;
    }

    public static void main(String[] args) {
        String input = "[[1,2,3],[4,5,6],[7,8,9]]";
        String output = new Topic498(input).run();
        System.out.println("input: " + input);
        System.out.println("output: " + output);
    }
}
